import pay from "../../../utils/pay"
import { showModal, showToast } from "../../../utils"
import request from "../../../utils/request"

export default {
	//根据订单号和相应的支付参数发起支付
	orderPay(detail) {
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: "加载中",
				mask: true,
			})
			pay
				.handlePay({
					ordNo: detail.ordNo,
					...JSON.parse(detail.payInfo),
				})
				.then(() => {
					resolve()
				})
				.catch((err) => {
					console.log(err, "err")
					showToast.error(err)
					reject(err)
				})
		})
	},
	//根据订单状态，返回相应的class类
	getClassByStatus(status) {
		// Paid:已支付,Send:已发货,Success:已完成,
		if (["Paid", "Send", "Success"].includes(status)) {
			return "success"
			//WaitPay:待支付,Refund:已退款
		} else if (["WaitPay", "Refund"].includes(status)) {
			return "warning"
		} else {
			return "info"
		}
	},
	//取消订单
	cancelOrder(detail) {
		return new Promise(async (resolve, reject) => {
			if (!await showModal({
				title: "提示",
				content: "确定要取消该订单吗？",
			})) {
				reject()
				return
			}
			request({
				url: "ord/cancelOrd",
				data: {
					ordNo: detail.ordNo,
				},
				errorHandle: true,
				showLoading: true,
			}).then(data => {
				resolve()
			}).catch(err => {
				reject(err)
			})
		})
	},
}
